<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * $Id: market_pr.php 172 2011-11-15 13:13:55Z leikou@163.com $
 */

/**
 * market module.
 *
 */

class Market_pr extends OA_Controller
{
    const CATAGORY = OA_CONST_CATAGORY::MARKETING;


    public function __construct()
    {
        parent::__construct();

        $this->load->library('user_agent');
    }


    /**
     * @name 学校列表
     * @desc 营销模块，按学校管理--学校列表
     */
    public function school_list()
    {
        // Get schools
        $schools = array();
        $query = $this->db
            ->from('school')
            ->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
            ->get();
        foreach($query->result_array() as $row)
        {
            $schools[$row['area_id']][] = $row;
        }

        // Get area
        $areas = array();
        $query = $this->db->from('area')->get();
        foreach($query->result_array() as $row)
        {
            $areas[$row['id']] = $row;
        }

        $this->load->view('market_school_list', array(
            'schools' => $schools,
            'areas' => $areas,
        ));
    }


    /**
     * @name 添加学校页面加载
     * @desc 营销模块，按学校管理--显示添加学校页面
     */
    public function school_add_page()
    {
        $this->load->view('market_school_add', array(
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));    
    }


    /**
     * @name 添加学校
     * @desc 营销模块，按学校管理--添加学校
     * @method post
     */
    public function school_add()
    {
        list($safe_vars) = $this->check(array(
            'area_id' => FC_int('区县')->required(),
            'name' => FC_chs('学校名')->min(2)->max(5)->required(),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));

        $this->db->insert('school', array(
            'name' => $safe_vars['name'],
            'area_id' => $safe_vars['area_id'],
            'flag' => OA_CONST_SCHOOL::FLAG_NORMAL,
        ));

        $this->load->view('info', array(
            'message' => '添加成功',
            'url' => $safe_vars['return_url'],
        ));
    }


    /**
     * @name 编辑学校页面加载
     * @desc 营销模块，按学校管理--显示编辑学校页面
     */
    public function school_edit_page()
    {
        list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
        ));

        $school = $this->db
            ->from('school')
            ->where('id', $safe_vars['school_id'])
            ->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
            ->get()
            ->row_array();
        if( ! $school)
        {
            show_error('很抱歉，你要编辑的学校不存在');
        }

        $this->load->view('market_school_detail', array(
            'school' => $school,
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));    
    }


    /**
     * @name 编辑学校
     * @desc 营销模块，按学校管理--编辑学校（区域，校名）
     * @method post 
     */
    public function school_edit()
    {
        list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
            'area_id' => FC_int('区县')->required(),
            'name' => FC_chs('学校名')->min(2)->max(5)->required(),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));

        $school = $this->db
            ->from('school')
            ->where('id', $safe_vars['school_id'])
            ->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
            ->get()
            ->row_array();
        if( ! $school)
        {
            show_error('很抱歉，你要编辑的学校不存在');
        }

        $this->db->where('id', $school['id'])->update('school', array(
            'name' => $safe_vars['name'],
            'area_id' => $safe_vars['area_id'],
        ));

        $this->load->view('info', array(
            'message' => '修改成功',
            'url' => $safe_vars['return_url'],
        ));
    }


    /**
     * @name 学校老师详情列表
     * @desc 营销模块，按学校管理--学校老师详情列表
     */
    public function school_teacher_list()
    {
        list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
            'page' => FC_int('页码', 1)->min(1),
        ));

        $school = $this->db
            ->from('school')
            ->where('id', $safe_vars['school_id'])
            ->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
            ->get()
            ->row_array();
        if( ! $school)
        {
            show_error('很抱歉，你要查看的学校不存在');
        }

        // Get teacher
        $teacher_obj = $this->db
            ->from('teacher')
            ->where('school_id', $school['id']);
        $teacher_count_obj = clone $teacher_obj;

        // Record count
        $record_count = $teacher_count_obj->count_all_results();

        // Page process
        $this->load->helper('page');
        $pagination = oa_page(
            $safe_vars['page'], 
            $record_count, 
            $this->config->item('per_page')
        );
        
        $teachers = $teacher_obj
            ->order_by('grade', 'desc')
            ->order_by('class', 'desc')
            ->limit($this->config->item('per_page'), $pagination['record_offset'])
            ->get()
            ->result_array();

        // TODO Get students count
        $students = array();

        $this->load->view('market_school_teacher_list', array(
            'school' => $school,
            'teachers' => $teachers,
            'students' => $students,
            'pagination' => $pagination,
            'gender_setting' => OA_CONST_USER::$GENDER,
            'discipline_setting' => OA_CONST_TEACHER::$DISCIPLINE,
            'class_type' => OA_CONST_TEACHER::$CLASS_TYPE,
        ));
    }


    /**
     * @name 添加教师页面
     * @desc 营销模块，按学校管理--加载添加教师页面
     */
    public function teacher_add_page()
    {
        list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required()
        ));

        // Get market user
        $market_users = $this->db
            ->select('u.id, u.username, u.name')
            ->from('role AS r')
            ->join('user_role AS ur', 'r.id=ur.r_id')
            ->join('user AS u', 'ur.u_id=u.id')
            ->where('r.catagory_id', OA_CONST_CATAGORY::MARKETING)
            ->where('u.status', OA_CONST_USER::STATUS_NORMAL)
            ->get()
            ->result_array();

        $this->load->view('market_teacher_add', array(
            'school_id' => $safe_vars['school_id'],
            'market_users' => $market_users,
            'class_type' => OA_CONST_TEACHER::$CLASS_TYPE,
            'discipline_setting' => OA_CONST_TEACHER::$DISCIPLINE,
            'gender_setting' => OA_CONST_USER::$GENDER,
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));
    }


    /**
     * @name 添加教师
     * @desc 营销模块，按学校管理--添加教师
     * @method post
     */
    public function teacher_add()
    {
        // ------------ check user input begin --------------
        list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
            'name' => FC_chs('姓名')->min(2)->max(4)->strict()->required(),
            'gender' => FC_int('性别')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_USER::$GENDER))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'birthday' => FC_date('出生年月', 'Y-m-d')->strict(),
            'job' => FC_chs('职务')->min(2)->max(6)->strict(),
            'grade' => FC_int('年级')->min(2011)->max(2040),
            'class' => FC_int('班级')->min(1)->max(40),
            'class_type' => FC_int('班别')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_TEACHER::$CLASS_TYPE))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'mobile' => FC_mobile('手机')->strict(),
            'telphone' => FC_phone('电话')->strict()->callback(function($v) {
                $v = str_replace(array(' ', '-'), array('', ''), $v);
                return array(TRUE, $v);   
            }),
            'discipline' => FC_int('任课')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_TEACHER::$DISCIPLINE))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'deduct' => FC_int('已提成', 0)->min(0),
            'level' => FC_int('等级')->min(1)->max(6)->required(),
            'last_maintain_date' => FC_date('上次跟进日期', 'Y-m-d')->strict(),
            'feedback' => FC_str('生活/反馈短信')->max(64),
            'festival_gift' => FC_str('节日礼物')->max(64),
            'bank_account' => FC_str('银行卡号')->max(16)->strict(),
            'contactor_id' => FC_int('营销联系人'),
            'area_manager_id' => FC_int('片区负责人'),
            'comment' => FC_str('备注')->max(300),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));

        // Check school
        $count = $this->db
            ->from('school')
            ->where('id', $safe_vars['school_id'])
            ->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
            ->count_all_results();
        if( ! $count)
        {
            show_error('学校不存在');
        }

        // Check contactor and manager
        $user_id_list = array();
        if($safe_vars['contactor_id'])
        {
            $user_id_list[] = $safe_vars['contactor_id'];
        }
        if($safe_vars['area_manager_id'])
        {
            $user_id_list[] = $safe_vars['area_manager_id'];
        }
        $user_id_list = array_unique($user_id_list);
        if($user_id_list)
        {
            $count = $this->db
                ->from('user')
                ->where_in('id', $user_id_list)
                ->where('status', OA_CONST_USER::STATUS_NORMAL)
                ->count_all_results();
            if($count != count($user_id_list))
            {
                show_error('很抱歉，你选择的营销联系人/片区负责人不存在');
            }
        }

        // ------------ check user input end -----------------
        $this->db->trans_start();

        $this->db->insert('teacher', array(
            'name' => $safe_vars['name'],
            'gender' => $safe_vars['gender'],
            'birthday' => $safe_vars['birthday'],
            'job' => $safe_vars['job'],
            'discipline' => $safe_vars['discipline'],
            'grade' => $safe_vars['grade'],
            'class' => $safe_vars['class'],
            'class_type' => $safe_vars['class_type'],
            'telphone' => $safe_vars['telphone'],
            'mobile' => $safe_vars['mobile'],
            'level' => $safe_vars['level'],
            'bank_account' => $safe_vars['bank_account'],
            'deduct' => $safe_vars['deduct'],
            'school_id' => $safe_vars['school_id'],
            'contactor_id' => $safe_vars['contactor_id'],
            'area_manager_id' => $safe_vars['area_manager_id'],
        ));
        $teacher_id = $this->db->insert_id();

        $this->db->insert('teacher_maintain', array(
            'teacher_id' => $teacher_id,
            'feedback' => $safe_vars['feedback'],
            'festival_gift' => $safe_vars['festival_gift'],
            'last_maintain_date' => $safe_vars['last_maintain_date']
        ));

        if($safe_vars['comment'])
        {
            $this->db->insert('teacher_maintain_comment', array(
                'teacher_id' => $teacher_id,
                'u_id' => $this->session->get_uid(),
                'comment' => $safe_vars['comment'],
                'dateline' => time(),
            ));
        }

        $this->db->trans_complete();

        $this->load->view('info', array(
            'message' => '添加成功',
            'url' => $safe_vars['return_url'],
        ));
    }


    /**
     * @name 教师详细信息 
     * @desc 营销模块，按学校管理--教师详细信息展示
     */
    public function teacher_detail()
    {
        list($safe_vars) = $this->check(array(
            'teacher_id' => FC_int('教师编号')->required(),
        ));

        // Get teacher
        $teacher = $this->db
            ->from('teacher')
            ->where('id', $safe_vars['teacher_id'])
            ->get()
            ->first_row('array');
        if( ! $teacher)
        {
            show_error('您要查看的教师不存在');
        }

        // Get teacher maintain
        $teacher_maintain = $this->db
            ->from('teacher_maintain')
            ->where('teacher_id', $teacher['id'])
            ->get()
            ->first_row('array');

        // Get teacher maintain comments
        $comments = $this->db
            ->select('c.*, u.username, u.name')
            ->from('teacher_maintain_comment AS c')
            ->join('user AS u', 'c.u_id = u.id')
            ->where('teacher_id', $teacher['id'])
            ->order_by('c.dateline', 'desc')
            ->get()
            ->result_array();

        // Get market user
        $market_users = $this->db
            ->select('u.id, u.username, u.name')
            ->from('role AS r')
            ->join('user_role AS ur', 'r.id=ur.r_id')
            ->join('user AS u', 'ur.u_id=u.id')
            ->where('r.catagory_id', OA_CONST_CATAGORY::MARKETING)
            ->where('u.status', OA_CONST_USER::STATUS_NORMAL)
            ->get()
            ->result_array();

        $this->load->view('market_teacher_detail.tpl', array(
            'teacher' => $teacher,
            'teacher_maintain' => $teacher_maintain,
            'comments' => $comments,
            'market_users' => $market_users,
            'class_type' => OA_CONST_TEACHER::$CLASS_TYPE,
            'discipline_setting' => OA_CONST_TEACHER::$DISCIPLINE,
            'gender_setting' => OA_CONST_USER::$GENDER,
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));
    }


    /**
     * @name 编辑教师
     * @desc 营销模块，按学校管理--编辑教师
     * @method post
     */
    public function teacher_edit()
    {
        // ------------ check user input begin --------------
        list($safe_vars) = $this->check(array(
            'teacher_id' => FC_int('教师编号')->required(),
            'name' => FC_chs('姓名')->min(2)->max(4)->strict()->required(),
            'gender' => FC_int('性别')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_USER::$GENDER))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'birthday' => FC_date('出生年月', 'Y-m-d')->strict(),
            'job' => FC_chs('职务')->min(2)->max(6)->strict(),
            'grade' => FC_int('年级')->min(2011)->max(2040),
            'class' => FC_int('班级')->min(1)->max(40),
            'class_type' => FC_int('班别')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_TEACHER::$CLASS_TYPE))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'mobile' => FC_mobile('手机')->strict(),
            'telphone' => FC_phone('电话')->strict()->callback(function($v) {
                $v = str_replace(array(' ', '-'), array('', ''), $v);
                return array(TRUE, $v);   
            }),
            'discipline' => FC_int('任课')->callback(function($v) {
                $valid = ($v === NULL || array_key_exists($v, OA_CONST_TEACHER::$DISCIPLINE))
                    ? TRUE : FALSE;
                return array($valid, $v);
            }),
            'deduct' => FC_int('已提成', 0)->min(0),
            'level' => FC_int('等级')->min(1)->max(6)->required(),
            'last_maintain_date' => FC_date('上次跟进日期', 'Y-m-d')->strict(),
            'feedback' => FC_str('生活/反馈短信')->max(64),
            'festival_gift' => FC_str('节日礼物')->max(64),
            'bank_account' => FC_str('银行卡号')->max(16)->strict(),
            'contactor_id' => FC_int('营销联系人'),
            'area_manager_id' => FC_int('片区负责人'),
            'comment' => FC_str('备注')->max(300),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));

        // Check teacher 
        $count = $this->db
            ->from('teacher')
            ->where('id', $safe_vars['teacher_id'])
            ->count_all_results();
        if( ! $count)
        {
            show_error('您要编辑的教师不存在');
        }

        // Check contactor and manager
        $user_id_list = array();
        if($safe_vars['contactor_id'])
        {
            $user_id_list[] = $safe_vars['contactor_id'];
        }
        if($safe_vars['area_manager_id'])
        {
            $user_id_list[] = $safe_vars['area_manager_id'];
        }
        $user_id_list = array_unique($user_id_list);
        if($user_id_list)
        {
            $count = $this->db
                ->from('user')
                ->where_in('id', $user_id_list)
                ->where('status', OA_CONST_USER::STATUS_NORMAL)
                ->count_all_results();
            if($count != count($user_id_list))
            {
                show_error('很抱歉，你选择的营销联系人/片区负责人不存在');
            }
        }

        // ------------ check user input end -----------------

        $this->db->trans_start();

        $teacher_id = $safe_vars['teacher_id'];
        $this->db->where('id', $teacher_id)
            ->update('teacher', array(
                'name' => $safe_vars['name'],
                'gender' => $safe_vars['gender'],
                'birthday' => $safe_vars['birthday'],
                'job' => $safe_vars['job'],
                'discipline' => $safe_vars['discipline'],
                'grade' => $safe_vars['grade'],
                'class' => $safe_vars['class'],
                'class_type' => $safe_vars['class_type'],
                'telphone' => $safe_vars['telphone'],
                'mobile' => $safe_vars['mobile'],
                'level' => $safe_vars['level'],
                'bank_account' => $safe_vars['bank_account'],
                'deduct' => $safe_vars['deduct'],
                'contactor_id' => $safe_vars['contactor_id'],
                'area_manager_id' => $safe_vars['area_manager_id'],
            ));

        $this->db->where('teacher_id', $teacher_id)
            ->update('teacher_maintain', array(
                'feedback' => $safe_vars['feedback'],
                'festival_gift' => $safe_vars['festival_gift'],
                'last_maintain_date' => $safe_vars['last_maintain_date']
            ));

        if($safe_vars['comment'])
        {
            $this->db->insert('teacher_maintain_comment', array(
                'teacher_id' => $teacher_id,
                'u_id' => $this->session->get_uid(),
                'comment' => $safe_vars['comment'],
                'dateline' => time(),
            ));
        }

        $this->db->trans_complete();

        $this->load->view('info', array(
            'message' => '编辑成功',
            'url' => $safe_vars['return_url'],
        ));
    }


    /**
     * @name 教师联系人列表
     * @desc 营销模块，按联系人管理--教师联系人列表
     */
    public function teacher_contactor_list()
    {
        $contactors = $this->db
            ->select('u.id, u.username, u.name')
            ->from('teacher AS t')
            ->join('user AS u', 't.contactor_id = u.id')
            ->where('u.status', OA_CONST_USER::STATUS_NORMAL)
            ->group_by('u.id')
            ->get()
            ->result_array();

        $this->load->view('market_teacher_contactor_list', array(
            'contactors' => $contactors,
        ));
    }


    /**
     * @name 联系人对应教师列表
     * @desc 营销模块，按联系人管理--联系人所有教师列表
     */
    public function contactor_teacher_list()
    {
        list($safe_vars) = $this->check(array(
            'contactor_id' => FC_int('联系人编号')->required(),
            'page' => FC_int('页码', 1)->min(1),
        ));

        // Get contactor info
        $contactor = $this->db
            ->select('id, username, name')
            ->from('user')
            ->where('id', $safe_vars['contactor_id'])
            ->where('status', OA_CONST_USER::STATUS_NORMAL)
            ->get()
            ->first_row('array');
        if( ! $contactor)
        {
            show_error('很抱歉，您要查看的联系人不存在');
        }

        // Get teacher
        $teacher_obj = $this->db
            ->select('t.*, s.name AS school_name')
            ->from('teacher AS t')
            ->join('school AS s', 't.school_id = s.id')
            ->where('contactor_id', $safe_vars['contactor_id']);
        $teacher_count_obj = clone $teacher_obj;

        // Record count
        $record_count = $teacher_count_obj->count_all_results();

        // Page process
        $this->load->helper('page');
        $pagination = oa_page(
            $safe_vars['page'], 
            $record_count, 
            $this->config->item('per_page')
        );
        
        $teachers = $teacher_obj
            ->order_by('school_id', 'desc')
            ->order_by('grade', 'desc')
            ->order_by('class', 'desc')
            ->limit($this->config->item('per_page'), $pagination['record_offset'])
            ->get()
            ->result_array();

        // TODO Get students count
        $students = array();

        $this->load->view('market_contactor_teacher_list', array(
            'contactor' => $contactor,
            'teachers' => $teachers,
            'students' => $students,
            'pagination' => $pagination,
            'gender_setting' => OA_CONST_USER::$GENDER,
            'discipline_setting' => OA_CONST_TEACHER::$DISCIPLINE,
            'class_type' => OA_CONST_TEACHER::$CLASS_TYPE,
        ));
    }


    /**
     * @name 工作计划列表
     * @desc 营销模块，工作计划列表展示
     */
    public function working_plan_list()
    {
        $query = $this->db
            ->select(
                'id, DATE_FORMAT(begin_date, "%Y") AS year, '.
                'DATE_FORMAT(begin_date, "%m-%d") AS begin_date', FALSE 
            )
            ->from('working_plan')
            ->order_by('begin_date', 'desc')
            ->get();
        $working_plans = array();
        foreach($query->result_array() as $item)
        {
            $working_plans[$item['year']][] = $item;
        }

        $this->load->view('market_working_plan', array(
            'working_plans' => $working_plans,
        ));
    }


    /**
     * @name 添加工作计划页面
     * @desc 营销模块，加载添加工作计划页面
     */
    public function working_plan_add_page()
    {
        $this->load->view('market_working_plan_add', array(
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));
    }


    /**
     * @name 添加工作计划
     * @desc 营销模块，添加工作计划
     * @method post
     */
    public function working_plan_add()
    {
        list($safe_vars) = $this->check(array(
            'begin_date' => FC_date('开始日期', 'Y-m-d')->required(),
            'end_date' => FC_date('结束日期', 'Y-m-d')->required(),
            'contact' => FC_str('新建联系')->max(300)->required(),
            'teacher' => FC_str('跟进老师')->max(300)->required(),
            'name_list' => FC_str('收集名单')->max(300)->required(),
            'other' => FC_str('其它')->max(300)->required(),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));
        if($safe_vars['begin_date'] > $safe_vars['end_date'])
        {
            show_error('很抱歉，计划开始时间不能大于计划结束时间');
        }

        $this->db->insert('working_plan', array(
            'begin_date' => $safe_vars['begin_date'],
            'end_date' => $safe_vars['end_date'],
            'contact' => $safe_vars['contact'],
            'teacher' => $safe_vars['teacher'],
            'name_list' => $safe_vars['name_list'],
            'other' => $safe_vars['other'],
            'create_dateline' => time(),
            'create_uid' => $this->session->get_uid(),
        ));

        $this->load->view('info', array(
            'message' => '添加成功',
            'url' => $safe_vars['return_url'],
        ));
    }


    /**
     * @name 工作计划详细页面
     * @desc 营销模块，显示工作计划详细页面
     */
    public function working_plan_detail()
    {
        list($safe_vars) = $this->check(array(
            'plan_id' => FC_int('计划编号')->required(),
        ));

        $plan = $this->db
            ->from('working_plan')
            ->where('id', $safe_vars['plan_id'])
            ->get()
            ->first_row('array');
        if( ! $plan)
        {
            show_error('很抱歉，您要编辑的工作计划不存在');
        }

        $this->load->view('market_working_plan_detail', array(
            'plan' => $plan,
            'return_url' => $this->agent->referrer(),
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
        ));
    }


    /**
     * @name 编辑工作计划
     * @desc 营销模块，编辑工作计划
     * @method post
     */
    public function working_plan_edit()
    {
        list($safe_vars) = $this->check(array(
            'plan_id' => FC_int('计划编号')->required(),
            'begin_date' => FC_date('开始日期', 'Y-m-d')->required(),
            'end_date' => FC_date('结束日期', 'Y-m-d')->required(),
            'contact' => FC_str('新建联系')->max(300)->required(),
            'teacher' => FC_str('跟进老师')->max(300)->required(),
            'name_list' => FC_str('收集名单')->max(300)->required(),
            'other' => FC_str('其它')->max(300)->required(),
            'return_url' => FC_str('返回地址', '')->max(255),
        ));
        if($safe_vars['begin_date'] > $safe_vars['end_date'])
        {
            show_error('很抱歉，计划开始时间不能大于计划结束时间');
        }

        // Check plan
        $plan = $this->db
            ->from('working_plan')
            ->where('id', $safe_vars['plan_id'])
            ->get()
            ->first_row('array');
        if( ! $plan)
        {
            show_error('很抱歉，您要编辑的工作计划不存在');
        }

        $this->db->where('id', $plan['id'])->update('working_plan', array(
            'begin_date' => $safe_vars['begin_date'],
            'end_date' => $safe_vars['end_date'],
            'contact' => $safe_vars['contact'],
            'teacher' => $safe_vars['teacher'],
            'name_list' => $safe_vars['name_list'],
            'other' => $safe_vars['other'],
        ));

        $this->load->view('info', array(
            'message' => '编辑成功',
            'url' => $safe_vars['return_url'],
        ));
    }
}


/* End of file: market.php */
/* Location: ./application/controllers/market.php */
